package BankAccount;

public class CurrentAccount extends BankAccount{
    private double odLimit = 1000.0;

    public CurrentAccount() {
    }

    public CurrentAccount(String accNumber, String accName) {
        super(accNumber, accName);
    }

    public CurrentAccount(String accNumber, String accName, double balance) {
        super(accNumber, accName);
        this.setBalance(balance);
    }

    /**
     * Current account has an overdraft, thus its balance can smaller than zero.
     * @param balance the balance to be set.
     */
    @Override
    public void setBalance(double balance) {
        if (balance >= 0) super.setBalance(balance);
        else if (-balance < odLimit) super.setBalance(balance);
        else System.out.printf("Invalid balance %f. Balance overdraft.\n", balance);
    }

    public void setOdLimit(double newLimit) { this.odLimit = newLimit; }

    public double getOdLimit() { return this.odLimit; }

    @Override
    public void withDraw(double amount) {
        if (amount < 0.0) System.out.printf("Invalid amount %.2f\n", amount);
        else{
            double left = getBalance() - amount;
            if (left >= 0.0) super.withDraw(left);
            else if (-left <= odLimit) super.withDraw(left);
            else System.out.printf("Balance: %.2f $\nWARNING! You are over draft! Withdraw operation failed.\n", getBalance());
        }
    }

    /**
     * A beautiful print method of CurrentAccount class.
     * @return
     */
    @Override
    public String toString() {
        String str = "";
        String filler = " ";
        int frameLen = 65;
        int frameWidth = 10;

        for (int i = 1; i <= frameWidth; i++) {
            String lineText = "";
            filler = " ";
            if (i == 1 || i == frameWidth) {
                for (int j = 0; j < frameLen; j++) {
                    lineText += "=";
                }
            }
            else {
                lineText += "||";
                String content = "";
                int InsertPos;
                if (i == 2) content = "JMRCB";
                if (i == 3) {
                    filler = "-";
                    content = "Current Account";
                }
                if (i == 4) {
                    content = "ACCOUNT NUMBER:";
                    int limit = content.length() + String.valueOf(getAccNumber()).length();
                    for (int j = limit; j < frameLen / 2; j++) {
                        content += filler;
                    }
                    content += getAccNumber();
                }
                if (i == 6) {
                    content = "ACCOUNT NAME:";
                    int limit = content.length() + String.valueOf(getAccName()).length();
                    for (int j = limit; j < frameLen / 2; j++) {
                        content += filler;
                    }
                    content += getAccName();
                }
                if (i == 8) {
                    content = "ACCOUNT BALANCE:";
                    int limit = content.length() + String.format("%.2f", getBalance()).length() + 2;
                    for (int j = limit; j < frameLen / 2; j++) {
                        content += filler;
                    }
                    content += String.format("%.2f$", getBalance());
                }

                InsertPos = ((frameLen - content.length()) / 2);
                for (int j = 1; j < InsertPos; j++) {
                    lineText += filler;
                }
                lineText += content;
                for (int j = lineText.length(); j < frameLen - 2; j++) {
                    lineText += filler;
                }
                lineText += "||";

            }
            lineText += '\n';
            str += lineText;
        }

        return str;
    }
}
